﻿function init() {
    getmenu();
    if ($.getPara('op') == 'edit') {
        $('#opbtn').show();
    }
}

function exit() {
    parent.$.closePage();
}

//#region 加载菜单数据
function getmenu() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryMenuList",
        success: function (data) {
            if (data && data.length > 0) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.id = data[i].MENU_ID;
                        if (lang.SysLanguage == "en") {
                            obj.text = data[i].ENMENU_NAME;
                        }
                        else {
                            obj.text = data[i].MENU_NAME;
                        }
                        obj.MENU_IMG = data[i].MENU_IMG;
                        obj.MENU_TYPE = data[i].MENU_TYPE;
                        obj.TARGET = data[i].TARGET;
                        obj.SORTCODE = data[i].SORTCODE;
                        obj.NAVIGATEURL = data[i].NAVIGATEURL;
                        getsubmenu(data, data[i].MENU_ID, obj);
                        _data.push(obj);
                    }
                }
                _menudata = data;
                $('#rolerights').tree('loadData', _data);
                $('#rolerights').tree({
                    cascadeCheck: false,
                    onCheck: function (node, checked) {
                        if (checked) {
                            var parentNode = $("#rolerights").tree('getParent', node.target);
                            if (parentNode != null) {
                                $("#rolerights").tree('check', parentNode.target);
                            }
                        } else {
                            var childNode = $("#rolerights").tree('getChildren', node.target);
                            if (childNode.length > 0) {
                                for (var i = 0; i < childNode.length; i++) {
                                    $("#rolerights").tree('uncheck', childNode[i].target);
                                }
                            }
                        }
                    }
                });
                if ($.getPara('roleid') != '') {
                    bindrolerights();
                }
            }

        }
    });

    function getsubmenu(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.id = _d[i].MENU_ID;
                    if (lang.SysLanguage == "en") {
                        obj.text = _d[i].ENMENU_NAME;
                    } else {
                        obj.text = _d[i].MENU_NAME;
                    }
                    obj.MENU_IMG = _d[i].MENU_IMG;
                    obj.TARGET = _d[i].TARGET;
                    obj.SORTCODE = _d[i].SORTCODE;
                    obj.MENU_TYPE = _d[i].MENU_TYPE;
                    obj.NAVIGATEURL = _d[i].NAVIGATEURL;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubmenu(_d, _sdata[j].MENU_ID, _sdata[j]);
                }
            }
        }
    }
}

//#endregion

//#region 绑定角色权限
function bindrolerights() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryRoleRightsByID",
        model: function () {
            return $.getPara('roleid');
        },
        success: function (data) {
            if (data && data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    var node = $('#rolerights').tree('find', data[i].MENU_ID);
                    $('#rolerights').tree('check', node.target);
                }  
            }
        }
    });
}
//#endregion

//#region 保存
function save() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "EditRoleRights",
        model: function () {
            var obj = {};
            obj.ROLES_ID = $.getPara('roleid');
            var rightsarr = [];
            var menulist = $('#rolerights').tree('getChecked');
            if (menulist && menulist.length > 0) {
                for (var i = 0; i < menulist.length; i++) {
                    rightsarr.push(menulist[i].id);
                }
            }
            obj.RoleRightList = rightsarr;
            return $.toJSON(obj)
        },
        success: function (data) {
            if (data) {
                // $.alert(lang.pemission_assignment_complete, "", function () { parent.$.closePage(); });
                alert(lang.pemission_assignment_complete);
                parent.$.closePage();
            }
        }
    });
}
//#endregion